<template>
  <div class="announcement-view">

    <div class="header">
      <h2>公告信息</h2>
    </div>

    <el-card shadow="hover">
      <el-collapse>
        <el-collapse-item
          v-for="announcement in announcementList"
          :key="announcement.id"
          :title="announcement.title"
        >
          <div>{{ announcement.content }}</div>
        </el-collapse-item>
      </el-collapse>
    </el-card>

  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { useAnnouncementStore } from '@/stores/api/announcement';

const announcementStore = useAnnouncementStore();
const announcementList = ref([]);

onMounted(async () => {
  try {
    const announcementData = await announcementStore.fetchAnnouncements();
    announcementList.value = announcementData;
  } catch (error: any) {
    ElMessage.error('加载数据失败: ' + error.message);
  }
});
</script>

<style scoped>
.announcement-view {
  width: 80%;
  margin: 40px auto 0 auto;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.header h2 {
  font-size: 24px;
  color: #333;
  flex-grow: 1;
  text-align: center;
}

.announcement-view .el-card {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.el-collapse {
  border: none;
  background-color: transparent;
}

.el-collapse-item__header {
  font-size: 16px;
  color: #333;
  padding: 10px 20px;
  background-color: transparent;
  border-bottom: 1px solid #ebeef5;
}

.el-collapse-item__content {
  padding: 10px 20px;
  font-size: 14px;
  color: #666;
  line-height: 1.6;
}
</style>